#include "stm32f10x.h"
uint8_t flag=0;  //记录信号上升沿/下降沿
uint32_t number; //记录定时器中断次数
uint32_t times;	 //记录回响信号持续时间us
/**
  * @brief 超声波传感器初始化
  * @param
  * @param 
  * @retval 
  */
void USC_Init(void)
{
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE); //使能AFIO
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);//GPIOA
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE); //定时器2
	
	//Trig
	GPIO_InitTypeDef GPIO_InitStructure;
	GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;      //推挽输出
	GPIO_InitStructure.GPIO_Pin=GPIO_Pin_6;             //GPIO_Pin6引脚
	GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;  	//默认50MHz
	GPIO_Init(GPIOA,&GPIO_InitStructure);
	GPIO_ResetBits(GPIOA,GPIO_Pin_6);//默认低电平
	//Echo
	GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;//选择浮空输入
	GPIO_InitStructure.GPIO_Pin=GPIO_Pin_7;
	GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
	GPIO_Init(GPIOA,&GPIO_InitStructure);
	
	//AFIO映射中断引脚
	GPIO_EXTILineConfig(GPIO_PortSourceGPIOA,GPIO_PinSource7);//外部中断源和中断通道

	//EXTI中断配置(Echo)
	EXTI_InitTypeDef  EXTI_InitStruct;
	EXTI_InitStruct.EXTI_Line=EXTI_Line7;//7号中断线echo使用端口7
	EXTI_InitStruct.EXTI_LineCmd=ENABLE; //使能
	EXTI_InitStruct.EXTI_Mode=EXTI_Mode_Interrupt;//触发中断，不是事件
	EXTI_InitStruct.EXTI_Trigger=EXTI_Trigger_Rising_Falling;//双边延触发
	EXTI_Init(&EXTI_InitStruct);
	
	//配置中断分组优先级
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
	//NVIC配置
	NVIC_InitTypeDef NVIC_InitStruct;
	NVIC_InitStruct.NVIC_IRQChannel=EXTI9_5_IRQn;//使用端口7，
	NVIC_InitStruct.NVIC_IRQChannelCmd=ENABLE;
	NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority=2;
	NVIC_InitStruct.NVIC_IRQChannelSubPriority=2;
	NVIC_Init(&NVIC_InitStruct);
	
//----------------------------------------------------------------------
	//配置定时器
	TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;
	TIM_TimeBaseStructInit(&TIM_TimeBaseInitStruct);//默认化参数配置
	TIM_TimeBaseInitStruct.TIM_ClockDivision=TIM_CKD_DIV1;//时钟分频1
	TIM_TimeBaseInitStruct.TIM_CounterMode=TIM_CounterMode_Up;//向上计数
	//定时器计时，每10us,计数值++
	TIM_TimeBaseInitStruct.TIM_Period=10-1;
	TIM_TimeBaseInitStruct.TIM_Prescaler=72-1;
	TIM_TimeBaseInit(TIM2,&TIM_TimeBaseInitStruct);
	
	//使能中断输出信号
	TIM_ITConfig(TIM2,TIM_IT_Update,ENABLE);
	
	//NVIC分配定时器中断优先级
	NVIC_InitStruct.NVIC_IRQChannel=TIM2_IRQn;//TIM2中断通道
	NVIC_InitStruct.NVIC_IRQChannelCmd=ENABLE;
	NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority=2;//抢占优先级
	NVIC_InitStruct.NVIC_IRQChannelSubPriority=2;//响应优先级
	NVIC_Init(&NVIC_InitStruct);
	
}
//开始发送数据的标志
void USC_start(void)
{
	GPIO_SetBits(GPIOA,GPIO_Pin_6);//高电平
}
//停止发送数据标志
void USC_stop(void)
{
	GPIO_ResetBits(GPIOA,GPIO_Pin_6);//低电平
}
//获取时间
uint32_t USC_GetTime(void)
{
	return times;
}
//内部的定时器中断函数
void TIM2_IRQHandler(void)
{
	if(TIM_GetITStatus(TIM2,TIM_FLAG_Update)==SET)
	{
		number++;//每次中断次数++
		TIM_ClearITPendingBit(TIM2,TIM_FLAG_Update);
	}
}

//外部中断服务函数(echo)
void EXTI9_5_IRQHandler(void)
{
	if(EXTI_GetITStatus(EXTI_Line7)==SET)//中断标志
	{
		if(flag==0)
		{
			//上升沿，回响电平开始，打开计数器
			number=0;
			times=0;
			flag=1;
			TIM_SetCounter(TIM2,0);//开始计数
			TIM_Cmd(TIM2,ENABLE);
		}else{
			//下降沿回响电平结束
			TIM_Cmd(TIM2,DISABLE);//关闭定时器
			flag=0;
			times=number*10;//每次中断10us,总次数乘以10us=总时间
		}
	EXTI_ClearITPendingBit(EXTI_Line7);
	}
}
